/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.newnews.bd;

import br.edu.ifal.newnews.modelo.Usuario;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ifal
 */
public class UsuarioBD {

    public void insereUsuario(Usuario usuario) throws SQLException {

        Connection c = new ConexaoBD().abreConexao();

        String sql = "insert into tbUsuario"
                + "(nome,sobrenome,senha,email)"
                + "values (?,?,?,?)";

        PreparedStatement stmt = null;

        try {
            stmt = c.prepareStatement(sql);


            stmt.setString(1, usuario.getNome());
            stmt.setString(2, usuario.getSobreNome());
            stmt.setString(3, usuario.getSenha());
            stmt.setString(4, usuario.getEmail()); 
            //stmt.setDate(6, new java.sql.Date(usuario.getDt_nascimento().getTimeInMillis()));
            // stmt.setString(5, usuario.getSexo());
             stmt.execute();
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(UsuarioBD.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            c.close();
        }
    }

    public void atualizaUsuario(Usuario c1) throws SQLException {

        Connection c = new ConexaoBD().abreConexao();

        String sql = " update tbUsuario set nome=?, email=?,sobrenome=? "
                + " where codigo=?";

        PreparedStatement stmt = c.prepareStatement(sql);

        stmt.setString(1, c1.getNome());
        stmt.setString(2, c1.getEmail());
        stmt.setString(3, c1.getSobreNome());
        stmt.setInt(4, c1.getCodigo());
       

        stmt.execute();
        stmt.close();
        c.close();

    }

    public void removeUsuario(Usuario usuario) throws SQLException {

        Connection c = new ConexaoBD().abreConexao();

        String sql = " delete from tbUsuario where senha= ? and nome=?";

        PreparedStatement stmt = c.prepareStatement(sql);
        stmt.setString(1,usuario.getSenha());
        stmt.setString(2,usuario.getNome());
        stmt.execute();
        stmt.close();
        c.close();

    }
    
    public Usuario buscaUsuarioPeloCodigo(int codigo) throws SQLException {
        Connection c = new ConexaoBD().abreConexao();

        String sql = "select * from tbUsuario where codigo= ?";
        PreparedStatement stmt = c.prepareStatement(sql);

        stmt.setInt(1, codigo);

        ResultSet rs = stmt.executeQuery();
        Usuario c1 = new Usuario();

        while (rs.next()) {
            

            c1.setNome(rs.getString("nome"));
            c1.setEmail(rs.getString("email"));
            c1.setSobreNome(rs.getString("sobrenome"));
            //c1.setSexo(rs.getString("sexo"));
        }
        rs.close();
        stmt.close();
        c.close();

        return c1;
    }
    
    public void atualizaSenha(Usuario c1) throws SQLException {

        Connection c = new ConexaoBD().abreConexao();

        String sql = " update tbUsuario set senha=? "
                + " where codigo=?";

        PreparedStatement stmt = c.prepareStatement(sql);

        stmt.setString(1, c1.getSenha());
        stmt.setInt(2, c1.getCodigo());
       

        stmt.execute();
        stmt.close();
        c.close();

    }

     public Usuario buscaUsuarioPeloNome(String nome) throws SQLException {
        Connection c = new ConexaoBD().abreConexao();

        String sql = "select * from tbUsuario where nome= ?";
        PreparedStatement stmt = c.prepareStatement(sql);

        stmt.setString(1, nome);

        ResultSet rs = stmt.executeQuery();
        Usuario c1 = new Usuario();

        while (rs.next()) {
            

            c1.setCodigo(rs.getInt("codigo"));
       
        }
        rs.close();
        stmt.close();
        c.close();

        return c1;
    }
}
